package com.witmed.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.witmed.dao.DoctorDao;
import com.witmed.entity.Department;
import com.witmed.entity.Doctor;
import com.witmed.entity.Hospital;
import com.witmed.util.DBUtils;

public class DoctorDaoImpl implements DoctorDao {

	private DBUtils dbUtil = new DBUtils();

	/**
	 * 修改医生信息
	 * 
	 * @param doctor
	 * @return
	 */
	@Override
	public int update(Doctor doctor) {
		int n = dbUtil.update(
				"update doctor set doctor_name = ?,doctor_password=?, doctor_sex=?,doctor_tel=?,doctor_identif_id=?,doctor_pictuer_path=?  where doctor_id = ?",
				doctor.getDoctorName(), doctor.getDoctorPassword(), doctor.getDoctorSex(), doctor.getDoctorTel(),
				doctor.getDoctorIdentifId(), doctor.getDoctorPicturePath(), doctor.getDoctorId());
		return n;
	}

	/**
	 * 医生登录
	 */
	@Override
	public List<Doctor> login(String doctorName, String doctorPassword) {
		StringBuffer sb = new StringBuffer();
		sb.append("select").append(" doctor_id,").append(" doctor_name,").append(" doctor_password,")
				.append(" doctor_pictuer_path").append(" from doctor")
				.append(" where doctor_name = ? and doctor_password = ?");

		ResultSet rs = dbUtil.query(sb.toString(), doctorName, doctorPassword);
		List<Doctor> list = new ArrayList<Doctor>();
		try {
			while (rs.next()) {
				Integer doctorId = rs.getInt("doctor_id");
				doctorName = rs.getString("doctor_name");
				doctorPassword = rs.getString("doctor_password");
				String doctorPictuerPath = rs.getString("doctor_pictuer_path");
				Doctor doctor = new Doctor(doctorId, doctorName, doctorPassword, null, null, null, doctorPictuerPath,
						null);
				list.add(doctor);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	/**
	 * 新增医生信息
	 * 
	 * chenxiangyang
	 */

	@Override
	public int insertDoctor(Doctor doctor) {
		// TODO Auto-generated method stub
		return dbUtil.update(
				"insert into doctor(doctor_id,doctor_name,doctor_password,doctor_sex,doctor_tel,doctor_identif_id,doctor_pictuer_path,department_id) values(?,?,?,?,?,?,?,?)",
				doctor.getDoctorId(), doctor.getDoctorName(), doctor.getDoctorPassword(), doctor.getDoctorSex(),
				doctor.getDoctorTel(), doctor.getDoctorIdentifId(),doctor.getDoctorPicturePath(), doctor.getDepartment().getDepartmentId());
	}

	/**
	 * 按条件查询
	 * 
	 * chenxiangyang
	 */

	@Override
	public List<Doctor> findDoctors(String genre, String genres) {
		List<Doctor> list = new ArrayList<Doctor>();
		StringBuffer sb = new StringBuffer();
		sb.append("select");
		sb.append(" doctor_id,");
		sb.append("doctor_tel,");
		sb.append("doctor_name,");
		sb.append("department_name,");
		sb.append("hospital_name");
		sb.append(" from");
		sb.append(" doctor");
		sb.append(" left join");
		sb.append(" department");
		sb.append(" on");
		sb.append(" department.department_id=doctor.department_id");
		sb.append(" left");
		sb.append(" join");
		sb.append(" hospital");
		sb.append(" on");
		sb.append(" department.hospital_id=hospital.hospital_id");

		if (genre == null) {
			// 没有条件 查询所有医生信息
			ResultSet rs = dbUtil.query(sb.toString());
			try {
				while (rs.next()) {
					int doctorId = rs.getInt("doctor_id");
					String doctorName = rs.getString("doctor_name");
					String doctorTel = rs.getString("doctor_tel");
					String departmentName = rs.getString("department_name");
					String hospitalName = rs.getString("hospital_name");
					Doctor doctor = new Doctor(doctorId, doctorName, null, null, doctorTel, null, null, null);
					// 创建部门对象，设置
					Department department = new Department(null, departmentName, null);
					// 创建医院对象，设置
					Hospital hospital = new Hospital(null, hospitalName);

					department.setHospital(hospital);

					doctor.setDepartment(department);

					list.add(doctor);

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		} else if ("doctor".equals(genre)) {
			// 根据医生姓名模糊查询
			sb.append(" where");
			sb.append(" doctor_name");
			sb.append(" like");
			sb.append(" concat('%',?,'%')");
			ResultSet rs = dbUtil.query(sb.toString(), genres);
			try {
				while (rs.next()) {
					int doctorId = rs.getInt("doctor_id");
					String doctorName = rs.getString("doctor_name");
					String doctorTel = rs.getString("doctor_tel");
					String departmentName = rs.getString("department_name");
					String hospitalName = rs.getString("hospital_name");
					Doctor doctor = new Doctor(doctorId, doctorName, null, null, doctorTel, null, null, null);
					// 创建部门对象，设置
					Department department = new Department(null, departmentName, null);
					// 创建医院对象，设置
					Hospital hospital = new Hospital(null, hospitalName);

					department.setHospital(hospital);

					doctor.setDepartment(department);

					list.add(doctor);

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		} else if ("department".equals(genre)) {
			// 根据科室名字模糊查询
			sb.append(" where");
			sb.append(" department_name");
			sb.append(" like");
			sb.append(" concat('%',?,'%')");
			ResultSet rs = dbUtil.query(sb.toString(), genres);
			try {
				while (rs.next()) {
					int doctorId = rs.getInt("doctor_id");
					String doctorName = rs.getString("doctor_name");
					String doctorTel = rs.getString("doctor_tel");
					String departmentName = rs.getString("department_name");
					String hospitalName = rs.getString("hospital_name");
					Doctor doctor = new Doctor(doctorId, doctorName, null, null, doctorTel, null, null, null);
					// 创建部门对象，设置
					Department department = new Department(null, departmentName, null);
					// 创建医院对象，设置
					Hospital hospital = new Hospital(null, hospitalName);

					department.setHospital(hospital);

					doctor.setDepartment(department);

					list.add(doctor);

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		} else if ("hospital".equals(genre)) {
			// 根据医院名字模糊查询
			sb.append(" where");
			sb.append(" hospital_name");
			sb.append(" like");
			sb.append(" concat('%',?,'%')");
			ResultSet rs = dbUtil.query(sb.toString(), genres);
			try {
				while (rs.next()) {
					int doctorId = rs.getInt("doctor_id");
					String doctorName = rs.getString("doctor_name");
					String doctorTel = rs.getString("doctor_tel");
					String departmentName = rs.getString("department_name");
					String hospitalName = rs.getString("hospital_name");
					Doctor doctor = new Doctor(doctorId, doctorName, null, null, doctorTel, null, null, null);
					// 创建部门对象，设置
					Department department = new Department(null, departmentName, null);
					// 创建医院对象，设置
					Hospital hospital = new Hospital(null, hospitalName);

					department.setHospital(hospital);

					doctor.setDepartment(department);

					list.add(doctor);

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return list;
	}

	/**
	 * 根据医生id删除医生信息
	 */
	@Override
	public int deleteByDoctorId(int doctorId) {
		// TODO Auto-generated method stub
		return dbUtil.update("delete from doctor where doctor_id=?", doctorId);
	}

	@Override
	public List<Doctor> phoneLogin(String doctorTel) {
		StringBuffer sb = new StringBuffer();
		sb.append("select").append(" doctor_id,").append(" doctor_name,").append(" doctor_password,")
				.append(" doctor_pictuer_path").append(" from doctor").append(" where doctor_tel = ?");
		ResultSet rs = dbUtil.query(sb.toString(), doctorTel);
		List<Doctor> list = new ArrayList<Doctor>();
		try {
			while (rs.next()) {
				Integer doctorId = rs.getInt("doctor_id");
				String doctorName = rs.getString("doctor_name");
				String doctorPassword = rs.getString("doctor_password");
				// doctorTel = rs.getString("doctor_tel");
				String doctorPictuerPath = rs.getString("doctor_pictuer_path");
				Doctor doctor = new Doctor(doctorId, doctorName, doctorPassword, null, doctorTel, null,
						doctorPictuerPath, null);
				list.add(doctor);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

}
